/* fafamt1a.p Print/Post Acquisition Cost/Basis Adjustments                   */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: fafamt1a.p 85192 2019-02-21 07:01:27Z atu                         $: */
/* Revision: 1.1.1.3    BY: Rajesh Lokre          DATE: 04/03/03  ECO: *M1RX* */
/* Revision: 1.1.1.5    BY: Paul Donnelly (SB)    DATE: 06/26/03  ECO: *Q00G* */
/* Revision: 1.1.1.6    BY: Alka Duseja           DATE: 06/07/04  ECO: *P252* */
/* Revision: 1.1.1.7    BY: Milind Shahane        DATE: 11/26/04  ECO: *P2WY* */
/* Revision: 1.1.1.11   BY: Sachin Deshmukh       DATE: 02/02/05  ECO: *P341* */
/* Revision: 1.1.1.12   BY: Austin Xie            DATE: 06/16/06  ECO: *R057* */
/* Revision: 1.1.1.13   BY: Austin Xie            DATE: 05/16/06  ECO: *R05S* */
/* Revision: 1.1.1.13   BY: Austin Xie            DATE: 06/13/06  ECO: *R067* */
/* Revision: 1.1.1.14   BY: Anil Sudhakaran       DATE: 08/15/07  ECO: *R0C6* */
/* Revision: 1.1.1.15   BY: Dilip Manawat         DATE: 02/26/07  ECO: *R0NP* */
/* Revision: 1.1.1.16   BY: Dinesh Dubey          DATE: 03/18/08  ECO: *R0PX* */
/* Revision: 1.1.1.17   BY: Sundeep Kalla         DATE: 12/17/08  ECO: *P6VK* */
/* Revision: 1.1.1.18   BY: Neil Curzon           DATE: 08/25/09  ECO: *R1Q3* */
/* Revision: 1.1.1.20   BY: Neil Curzon           DATE: 02/17/10  ECO: *R1Y3* */
/* Revision: 1.1.1.22   BY: Diyin Zhou            DATE: 07/08/10  ECO: *R21B* */
/* REVISION: 23537     LAST MODIFIED: 10/19/12    BY: ddo                     */
/* $Revision: 1.1.1.23 $ BY: Aaron Luo            DATE: 07/22/10  ECO: *R23T* */
/*-Revision end---------------------------------------------------------------*/
 
{us/mf/mfdtitle.i}
 
define input parameter l_postgl  like mfc_logical no-undo.
define input parameter l_effdate like glt_effdate format "99/99/9999"   no-undo.
define input parameter l_post_bk like fabk_id                            no-undo.
define input parameter l_printgl like mfc_logical no-undo.
 
define        variable l_fabd_post  like mfc_logical no-undo.
define        variable l_ok         like mfc_logical no-undo.
define        variable l_ref        like glt_ref no-undo.
define        variable l_curr       like en_curr no-undo.
define        variable l_function   as character no-undo.
define        variable l_onlytrf    like mfc_logical no-undo.
define        variable l_daybook    like glt_dy_code no-undo.
define        variable l_delta      like mfc_logical initial no no-undo.
define        variable l_print_ok   like mfc_logical no-undo.
define        variable l_primary_bk like fabk_id no-undo.
define shared variable l-detail     like mfc_logical no-undo.
define        variable seqToUse     as character no-undo.
/* VARIABLES FOR FA PERSISTENT PROCEDURES */
{us/gp/gprunpdf.i "fapl" "p"}
 
/* SHARED TEMP-TABLE tt_trsus */
define new shared temp-table tt_trsus no-undo
   field tt_asset   like fabd_fa_id
   field tt_fabk    like fabd_fabk_id
   field tt_onlytrf like mfc_logical.

{us/fa/fapsacc.i} /* Table Definition for tt_accdet */
form
   skip(.4)
   l_postgl  colon 30
   l_effdate colon 30
   l_printgl colon 30
   with frame a overlay row 10 side-labels width 80.

setFrameLabels(frame a:handle).
 
/* ---------------  Pre Processing Include  -------------- */
/* VARIABLES FOR GPGLEF1.I */
{us/gp/gpglefv.i}
 
/* NRM SEQUENCE GENERATOR */
{us/gp/gpnbrgen.i}
 
/* EAS PROCEDURES */
{us/gp/gprunpdf.i "gpglvpl" "p"}
 
/* DAYBOOK VARIABLES */
{us/gl/gldydef.i}
 
/* NRM VARIABLES */
{us/gl/gldynrm.i}
{us/px/pxmaint.i}
{us/px/pxphdef.i glsafxr} /* Define Handles for the programs */
{us/px/pxphdef.i gpgenseq}
{us/px/pxgetph.i gpgenseq}
 
/* GET ENTITY SECURITY INFORMATION */
{us/gl/glsec.i}
 
/* TEMP TABLE FOR ADDING AQUISITION COST POSTING LOGIC */
{us/fa/fapsmt.i}
repeat:
   if c-application-mode <> "API" 
   then do: 
      display
         l_postgl
         l_effdate
      with frame a.
      update
         l_printgl
      with frame a. 
   end. /* c-application-mode <> "API" */

   if l_printgl
      then 
   do:
      /* OUTPUT DESTINATION SELECTION */
      {us/gp/gpselout.i
         &printType                = "printer"
         &printWidth               = 132
         &pagedFlag                = " "
         &stream                   = " "
         &appendToFile             = " "
         &streamedOutputToTerminal = " "
         &withBatchOption          = "no"
         &displayStatementType     = 1
         &withCancelMessage        = "yes"
         &pageBottomMargin         = 6
         &withEmail                = "yes"
         &withWinprint             = "no"
         &defineVariables          = "yes"}
   end. /* IF l_printgl */  

   /* GET THE Primary POSTING BOOK */
   for first fabk_mstr
      where fabk_mstr.fabk_domain = global_domain and  fabk_post = yes and fabk_primary
      no-lock:
      l_primary_bk = fabk_mstr.fabk_id.
   end. /* FOR FIRST fabk_mstr */  
   /*
      assign tt_primary value, if primary set to 0,
      it is used to sort the record, since in many scenario, it needs to process primary book first
   */ 
   for each tt_pstast where tt_book = l_primary_bk
      exclusive-lock:
      assign 
         tt_pstast.tt_primary = 0.				
   end.
   for each tt_pstast
      no-lock
      break by tt_primary 
      by tt_effdt
      by tt_asset:
      l_fabd_post = no.
      /* Invoke procedure to get daybook for the FA book */
      {us/gp/gprunp.i "fapl" "p" "fa-get-daybookforpostingbook"
            "(input  tt_pstast.tt_entity,
              input  tt_book,
              output l_daybook)"}
      if not tt_uop
      then do:
         /* GENERATE JOURNAL ENTRIES INTO TEMP-TABLE */
         /* ADDED THIRTEENTH OUTPUT PARAMETER l_onlytrf */
         if tt_pstast.tt_cost <> 0
         then do:
            {us/bbi/gprun.i ""fapsmta.p""
               "(input tt_entity,
                 input tt_entity,
                 input tt_pstast.tt_asset,
                 input tt_pstast.tt_asset,
                 input tt_pstyrper,
                 input tt_effdt,
                 input l_daybook,
                 input tt_book,
                 input l_fabd_post,
                 input yes,
                 input tt_cost,
                 input-output table tt_accdet,
                 input yes, /* indicate if it's Acquisition post */
                 output l_ok,
                 output l_onlytrf)"}
            l_print_ok = yes.
         end. /* IF tt_book = l_post_bk */
         else 
         do:
            assign
               l_ok       = yes
               l_print_ok = no.
         end. /* ELSE DO */
      end. /* IF NOT tt_uop */
      else 
      do:
	  if tt_pstast.tt_cost <> 0
         then do:
         	run ip_uoppost
         	(input tt_pstast.tt_asset,
         	input tt_cost,
         	input tt_entity,
         	input tt_effdt,
         	input tt_pstyrper,
         	input l_daybook,
         	input tt_book,
         	output l_ok).
	  end.
 
      if l_ok
         then do:
            l_function = "post":U.
 
            if not can-find (first tt_trsus 
                                where tt_trsus.tt_asset = tt_pstast.tt_asset
                                and   tt_trsus.tt_fabk  = tt_pstast.tt_book)
            then do:
               create tt_trsus.
               assign
                  tt_trsus.tt_asset   = tt_pstast.tt_asset
                  tt_trsus.tt_fabk    = tt_pstast.tt_book
                  tt_trsus.tt_onlytrf = no.
 
            end. /* IF NOT CAN-FIND ... */
          /*
            {us/bbi/gprun.i ""faglbl2.p""
               "(input tt_effdt,
                 input l_post_bk,
                 input l_daybook,
                 input l_function,
                 input yes,
                 input l_onlytrf)"}
  
            /* for other books */
            for each fabk_mstr
               where fabk_domain = global_domain
               and fabk_post = yes
               and fabk_primary = no
               and fabk_delta   = no
               no-lock:
               for first fab_det
                  where fab_det.fab_fabk_id = fabk_mstr.fabk_id
                  and  fab_det.fab_fa_id = tt_pstast.tt_asset
                  and fab_det.fab_resrv = 0:
  
                  l_post_bk = fabk_mstr.fabk_id.
 
                  /* Invoke procedure to get daybook for the FA book */
                  {us/gp/gprunp.i "fapl" "p" "fa-get-daybookforpostingbook"
                        "(input  tt_pstast.tt_entity,
                          input  l_post_bk,
                          output l_daybook)"}
 
                 run ip_uoppost
                    (input tt_pstast.tt_asset,
                     input tt_cost,
                     input tt_entity,
                     input tt_effdt,
                     input tt_pstyrper,
                     input l_daybook,
                     input tt_book,
                     output l_ok).
 
                 if l_ok
                 then do:
 
                    {us/bbi/gprun.i ""faglbl2.p""
                       "(input tt_effdt,
                         input l_post_bk,
                         input l_daybook,
                         input l_function,
                         input yes,
                         input l_onlytrf)"}
 
                  end. /* if l_ok */
               end. /* for first fab_det */
            end. /* for each fabk_mstr */
            */
         end. /* if l_ok */
         l_print_ok = yes.
      end. /* ELSE DO */
 
      /* print the report. */
      if l_print_ok
      then do:
         l_print_ok = no.
         if l_printgl
         then do:
            /* PRINT ERROR/JOURNAL REPORT */
            {us/bbi/gprun.i ""fapsmtb.p""
				"(input l_ok,
				  input no,
				  input tt_effdt)"}
            if c-application-mode <> "API" then
               hide message no-pause.
         end. /* IF l_printgl */
 
         /* GENERATE THE GL REFERENCE TO BE STORED EVEN IF */
         /* GL AUDIT TRAIL IS NOT TO BE PRINTED            */
         else 
         do:
            /* l-detail WILL ALWAYS BE yes FOR ACQUISITION AND COST         */
            /* ADJUSTMENTS GL TRANSACTION, AS THESE SHOULD SHOW ONLY DETAIL */
 
            l-detail = yes.
            for each t-dep
               exclusive-lock
               break by tf-asset by tf-daybook:
 
               /* GENERATE ONE REFERENCE # FOR EACH ASSET */
               if first(tf-daybook)
               then do:
 
                  /* GET JOURNAL ENTRY REFERENCE NUMBER */
                  l_ref = "FA"
                        + substring(string(year(tt_effdt),"9999"),3,2)
                        + string(month(tt_effdt),"99")
                        + string(day(tt_effdt),"99").
 
                  seqToUse = {us/px/pxfunct.i &FUNCTION = 'get6DigitGLRefSeq'
                                 &PROGRAM  = 'gpgenseq.p'
                                 &HANDLE   = ph_gpgenseq}.
                  l_ref = substring(l_ref, 1, 8) + seqToUse.
                  {us/bbi/gprun.i ""gppstref.p""
                        "(input tt_effdt,
                          input """",
                          input-output l_ref)"}
 
                  /* GET ENTITY CURRENCY */
                  for first en_mstr
                     where en_domain = global_domain
                     and  en_entity = tf-entity
                     no-lock:
                     l_curr = en_curr.
                  end. /* FOR FIRST en_mstr */
 
               end. /* IF FIRST(tf-entity) */
 
               assign
                  tf-ref  = l_ref
                  tf-curr = l_curr.
            end.  /* FOR EACH t-dep */
 
         end. /* ELSE DO */
      end. /* IF l_print_ok */

      if c-application-mode = "API" then assign l_ok = yes. 
      if l_ok
      then do:
         l_function = "post":U.
 
         {us/bbi/gprun.i ""faglbl2.p""
   			"(input tt_effdt,
   			  input l_post_bk,
   			  input tt_daybk,
   			  input l_function,
   			  input yes,
   			  input l_onlytrf)"}
	     if c-application-mode <> "API" then	
         	hide message no-pause.
      end. /* IF l_ok */
 
      if not l_ok
      then do:
         /* CANNOT POST - ERRORS HAVE BEEN DETECTED*/
         {us/bbi/pxmsg.i &MSGNUM=3266 &ERRORLEVEL=1}
      end. /* IF not l_ok */
      /* DELETE GL DETAIL LINES FOR CURRENT ASSET */
      empty temp-table t-dep.
      /* Update FA ACQUISITION post flag */
      if l_ok and l_postgl and lastkey <> keycode("F4") then
      do:
         define buffer bffa_mstr for fa_mstr.
         for first bffa_mstr where bffa_mstr.fa_id = tt_pstast.tt_asset
                               and bffa_mstr.fa_domain = global_domain
            exclusive-lock:
            if bffa_mstr.fa_acq_post = no then
            do:
               bffa_mstr.fa_acq_post = yes.
            end.
            bffa_mstr.fa_posted_puramt = fa_puramt.
         end.
      end.

      delete tt_pstast.
   end. /* FOR EACH tt_pstast */
   if l_printgl
      then 
   do:
      l_printgl = no.
      {us/bbi/mfreset.i}
      if c-application-mode <> "API" then
         pause 0.
   end. /* IF l_printgl */
end. /*REPEAT*/
 
 
PROCEDURE ip_uoppost:
 
   /* STORE POSTING DETAILS FOR UOP TYPE OF UN-POSTED ASSETS */
   define input  parameter p_asset   like fa_id               no-undo.
   define input  parameter p_cost    like fa_puramt           no-undo.
   define input  parameter p_entity  like fa_entity           no-undo.
   define input  parameter p_effdate like glt_effdate         no-undo.
   define input  parameter p_yrper   like fabd_det.fabd_yrper no-undo.
   define input  parameter p_daybook like dy_dy_code          no-undo.
   define input  parameter p_book    like fabk_id             no-undo.
   define output parameter p_ok      like mfc_logical         no-undo.
 
   define variable l_trnstype like glt_desc no-undo.
   define variable l_acctype  like faba_acctype no-undo.
   define variable l_sign     as character no-undo.
 
   assign
      l_trnstype = if can-find(first fa_mstr
                                where fa_mstr.fa_domain = global_domain and
                                fa_id   = p_asset
                               and   fa_post = no)
                   then
                      getTermLabel("ACQUISITION",24)
                   else
                      /* TRANSACTION TYPE- COST ADJUSTMENT */
                      getTermLabel("COST_ADJUSTMENT",24)
 
      /* PUT ASSET INTO BOOK - DEBIT ASSET ACCOUNT */
      /* IF AMOUNT IS NEGATIVE THEN CREDIT IT      */
      l_sign     = if p_cost > 0
                   then
                      "debit"
                   else
                      "credit"
      /* ASSET ACCOUNT */
      l_acctype  = "1".
 
   /* STORE DATA REQUIRED TO CREATE THE JOURNAL DETAILS */
   run ip_create_jrn
      (input p_asset,
      input l_acctype,
      input l_trnstype,
      input p_cost,
      input p_cost,
      input p_entity,
      input l_sign,
      input p_effdate,
      input p_yrper,
      input p_daybook,
      input p_book).
 
   /* CREDIT CONSTRUCTION IN PROGRESS     */
   /* IF AMOUNT IS NEGATIVE THEN DEBIT IT */
   assign
      l_sign    = if p_cost < 0
                  then
                     "debit"
                  else
                     "credit"
      /* CONSTRUCTION IN PROGRESS ACCOUNT */
      l_acctype = "4".
 
   /* STORE DATA REQUIRED TO CREATE THE JOURNAL DETAILS */
   run ip_create_jrn
      (input p_asset,
      input l_acctype,
      input l_trnstype,
      input p_cost,
      input p_cost,
      input p_entity,
      input l_sign,
      input p_effdate,
      input p_yrper,
      input p_daybook,
      input p_book).
 
 
   p_ok = not(can-find(first t-dep where tf-msgdesc <> "")).
 
END PROCEDURE. /* IP_UOPPOST */
 
/* STORE DATA REQUIRED TO CREATE THE JOURNAL DETAILS */
PROCEDURE ip_create_jrn:
 
   define input parameter p_asset    like fa_id               no-undo.
   define input parameter p_acctype  like faba_acctype        no-undo.
   define input parameter p_trnstype like faba_acctype        no-undo.
   define input parameter p_cost     like fa_puramt           no-undo.
   define input parameter p_curramt  like fa_puramt           no-undo.
   define input parameter p_entity   like fa_entity           no-undo.
   define input parameter p_sign     as   character           no-undo.
   define input parameter p_effdate  like glt_effdate         no-undo.
   define input parameter p_yrper    like fabd_det.fabd_yrper no-undo.
   define input parameter p_daybook  like dy_dy_code          no-undo.
   define input parameter p_book     like fabk_id          	no-undo.
 
   define variable l_messages  as character no-undo.
   define variable l_msglevels as character no-undo.
   define variable l_msgnbr    like msg_nbr initial 0 no-undo.
   define variable l_msgdesc   like msg_desc no-undo.
   define variable l_count     as integer   no-undo.
   define variable l_severity  as integer   no-undo.
   define variable l_result    like mfc_logical no-undo.
   define variable l_recno     as recid     no-undo.
   define variable l_acdesc    like ac_desc no-undo.
   define variable l_acct      like faba_acct no-undo.
   define variable l_sub       like faba_sub no-undo.
   define variable l_cc        like faba_cc no-undo.
   define variable l_proj      like faba_proj no-undo.
   define variable l_glseq     like faba_glseq no-undo.
   define variable l_msg_arg1  as character no-undo.
   define variable l_msg_arg2  as character no-undo.
   define variable l_msg_arg3  as character no-undo.
   define variable l_msg_arg4  as character no-undo.
   define variable l_msg_arg5  as character no-undo.
   define variable l_acctname  as character no-undo.
   define variable l_suspend   like mfc_logical no-undo.
   define variable l_transfer  like mfc_logical no-undo.
 
   for last faba_det
      where faba_det.faba_domain = global_domain
      and  faba_fa_id   = p_asset
      and  faba_acctype = p_acctype
      and  faba_fabk_id = p_book
      and can-find(first fabk_mstr
      where fabk_domain = global_domain
      and fabk_id     = faba_fabk_id
      and fabk_post   = yes)
      use-index faba_fa_id
      no-lock:
   end. /* FOR LAST faba_det */
 
   if available faba_det
   then
      assign
         l_acct     = faba_acct
         l_sub      = faba_sub
         l_cc       = faba_cc
         l_proj     = faba_proj
         l_glseq    = faba_glseq
         l_acctname = "faba_acct" + "_" + faba_acctype.
   else
      assign
         l_acct  = ""
         l_sub   = ""
         l_cc    = ""
         l_proj  = ""
         l_glseq = 0.
 
   /* INITIALIZE SETTINGS */
   {us/gp/gprunp.i "gpglvpl" "p" "initialize"}
 
   /* SET CONTROL FOR MESSAGE DISPLAY FROM VALIDATION PROCEDURE */
   {us/gp/gprunp.i "gpglvpl" "p" "set_disp_msgs"
      "(input false)"}
 
   /* SET GLTYPE VALIDATION TO "NO" IF FABA_DET IS NOT FOUND */
   if l_acct = "" then 
   do:
      {us/gp/gprunp.i "gpglvpl" "p" "set_acct_gltype_ver" "(input false)"}
   end.
   /* SET ENTITY VERIFICATION TO YES */
   {us/gp/gprunp.i "gpglvpl" "p" "set_coa_restriction_ver" "(input true, input p_entity)"}
   /* ACCT/SUB/CC/PROJ VALIDATION */
   {us/gp/gprunp.i "gpglvpl" "p" "validate_fullcode"
      "(input  l_acct,
        input  l_sub,
        input  l_cc,
        input  l_proj,
        input  l_acctname,
        output l_result)"}
 
   /* ONLY GET AND CHECK MSGS IF RESULT IS NOT TRUE */
   if not l_result
   then do:
      /* GET ERROR MESSAGES */
      {us/gp/gprunp.i "gpglvpl" "p" "get_msgs"
         "(output l_messages,
           output l_msglevels)"}
 
      /* GET ERROR MESSAGE ARGUMENTS FROM gpglvpl.p */
      {us/gp/gprunp.i "gpglvpl" "p" "get_msg_args"
         "(output l_msg_arg1,
           output l_msg_arg2,
           output l_msg_arg3,
           output l_msg_arg4,
           output l_msg_arg5
           )"}
 
 
      do l_count = 1 to num-entries(l_msglevels):
 
         l_severity = integer(entry(l_count,l_msglevels)).
         if l_severity = 3
         then
            leave.
 
      end. /* DO l_count = 1 TO NUM-ENTRIES */
 
      if l_severity = 3
      then do:
         l_msgnbr = integer(entry(l_count,l_messages)).
 
         {us/bbi/pxmsg.i &MSGNUM=l_msgnbr
                  &MSGBUFFER=l_msgdesc
                  &MSGARG1=l_msg_arg1
                  &MSGARG2=l_msg_arg2
                  &MSGARG3=l_msg_arg3
                  &MSGARG4=l_msg_arg4
                  &MSGARG5=l_msg_arg5}
 
      end. /* IF l_severity = 3 */
   end. /* IF NOT l_result */
 
 
   if available faba_det then 
   do:
      {us/gp/gprunp.i "fapl" "p" "fa-get-suspend"
                "(input  faba_fa_id,
                  input  faba_fabk_id,
                  input  p_yrper,
                  output l_suspend)"}
 
      {us/gp/gprunp.i "fapl" "p" "fa-get-transfer"
                "(input  faba_fa_id,
                  input  faba_fabk_id,
                  input  p_yrper,
                  output l_transfer)"}
   end.
 
   for first ac_mstr
      where ac_mstr.ac_domain = global_domain and  ac_code = l_acct
      no-lock:
   end. /* FOR FIRST ac_mstr */
 
   l_acdesc = if available ac_mstr
   then
      ac_desc
      else
      "".
 
   if not can-find (first tt_trsus 
                       where  tt_asset   = p_asset
                       and    tt_fabk    = p_book )
   then do:
      create tt_trsus.
      assign
         tt_asset = p_asset
         tt_fabk  = p_book.
      if l_suspend and l_transfer then assign tt_onlytrf = yes.
      else assign tt_onlytrf = no.
   end. /* IF NOT CAN-FIND ... */
 
   create t-dep.
   assign
      tf-entity  = p_entity
      tf-acct    = l_acct
      tf-sub     = l_sub
      tf-cc      = l_cc
      tf-project = l_proj
      tf-amt     = if (p_cost > 0 and p_sign = "credit") or
                         (p_cost < 0 and p_sign = "debit")
      then
                         (p_cost * -1)
                      else
                         p_cost
      tf-curramt    = if (p_cost > 0 and p_sign = "credit") or
                         (p_cost < 0 and p_sign = "debit")
                      then
                         (p_curramt * -1)
                      else
                         p_curramt
      tf-effdate    = p_effdate
      tf-asset      = p_asset
      tf-acdesc     = l_acdesc
      tf-msgdesc    = l_msgdesc
      tf-yrper      = p_yrper
      tf-daybook    = p_daybook
      tf-trnstype   = p_trnstype
      tf-glseq      = l_glseq
      tf-fabk		= p_book.
 
   if recid(t-dep) = -1 then .
   release t-dep.
 
END PROCEDURE. /* ip_create_jrn */
